import json
from threading import Lock

from hubstation.config.system_config import SystemConfig
from hubstation.constants import SystemConfigKey, MediaType, RssType
from hubstation.downloader import Downloader
from hubstation.helper.db_helper import DbHelper
from hubstation.indexer.Indexer import Indexer
from hubstation.searcher.searcher import Searcher


class task:
    dbHelper = None
    searcher = None
    downloader = None
    indexer = None

    def __init__(self):
        self.init_config()

    def init_config(self):
        self.dbHelper = DbHelper()
        self.searcher = Searcher()
        self.downloader = Downloader()
        self.indexer = Indexer()

    @property
    def default_rss_setting_tv(self):
        return SystemConfig().get(SystemConfigKey.DefaultRssSettingTV) or {}

    @property
    def default_rss_setting_mov(self):
        return SystemConfig().get(SystemConfigKey.DefaultRssSettingMOV) or {}

    def add_rss_task(self, mtype, name,
                     channel=None,
                     keyword=None,
                     rss_sites=None,
                     search_sites=None,
                     filter_restype=None,
                     filter_pix=None,
                     filter_rule=None,
                     filter_include=None,
                     filter_exclude=None,
                     save_path=None,
                     download_setting=None,
                     state="D",
                     rssid=None,
                     in_from=None,
                     user_name=None):
        """
        添加电影、电视剧订阅
        :param mtype: 类型，电影、电视剧、动漫
        :param name: 标题
        :param channel: 自动或手动
        :param keyword: 自定义搜索词
        :param rss_sites: 订阅站点列表，为空则表示全部站点
        :param search_sites: 搜索站点列表，为空则表示全部站点
        :param filter_restype: 质量过滤
        :param filter_pix: 分辨率过滤
        :param filter_rule: 关键字过滤
        :param filter_include: 包含关键字
        :param filter_exclude: 排除关键字
        :param save_path: 保存路径
        :param download_setting: 下载设置
        :param state: 添加订阅时的状态
        :param rssid: 修改订阅时传入
        :param in_from: 来源
        :param user_name: 用户名
        :return: 错误码：0 代表成功，错误信息
        """
        if not name:
            return -1, "标题或类型有误", None
        rss_sites = rss_sites or []
        if isinstance(rss_sites, str):
            rss_sites = rss_sites.split(",")
        search_sites = search_sites or []
        if isinstance(search_sites, str):
            search_sites = search_sites.split(",")
        filter_rule = int(filter_rule) if str(filter_rule).isdigit() else None
        download_setting = int(download_setting) if str(download_setting).replace("-", "").isdigit() else ""
        if channel == RssType.Auto:
            default_rss_setting = self.default_rss_setting_tv if mtype == MediaType.TV else self.default_rss_setting_mov
            if default_rss_setting:
                default_restype = default_rss_setting.get('restype')
                default_pix = default_rss_setting.get('pix')
                default_team = default_rss_setting.get('team')
                default_rule = default_rss_setting.get('rule')
                default_include = default_rss_setting.get('include')
                default_exclude = default_rss_setting.get('exclude')
                default_download_setting = default_rss_setting.get('download_setting')
                default_over_edition = default_rss_setting.get('over_edition')
                default_rss_sites = default_rss_setting.get('rss_sites')
                default_search_sites = default_rss_setting.get('search_sites')
                if not filter_restype and default_restype:
                    filter_restype = default_restype
                if not filter_pix and default_pix:
                    filter_pix = default_pix

                if not filter_rule and default_rule:
                    filter_rule = int(default_rule) if str(default_rule).isdigit() else None
                if not filter_include and default_include:
                    filter_include = default_include
                if not filter_exclude and default_exclude:
                    filter_exclude = default_exclude
                if not download_setting and default_download_setting:
                    download_setting = int(default_download_setting) \
                        if str(default_download_setting).replace("-", "").isdigit() else ""
                if not rss_sites and default_rss_sites:
                    rss_sites = default_rss_sites
                if not search_sites and default_search_sites:
                    search_sites = default_search_sites

        if rssid:
            self.delete_subscribe(mtype=MediaType.TV, rssid=rssid)
            code = self.dbhelper.insert_rss_tv(media_info=media_info,
                                           total=0,
                                           lack=0,
                                           state="R",
                                           rss_sites=rss_sites,
                                           search_sites=search_sites,
                                           over_edition=over_edition,
                                           filter_restype=filter_restype,
                                           filter_pix=filter_pix,
                                           filter_team=filter_team,
                                           filter_rule=filter_rule,
                                           filter_include=filter_include,
                                           filter_exclude=filter_exclude,
                                           save_path=save_path,
                                           download_setting=download_setting,
                                           fuzzy_match=1,
                                           keyword=keyword)
